package cn.tedu.hm.controller;
import cn.tedu.hm.pojo.dto.OrderMakeNewDTO;
import cn.tedu.hm.pojo.dto.OrderUpdateDTO;
import cn.tedu.hm.pojo.vo.OrderListItemVO;
import cn.tedu.hm.pojo.vo.OrderStandardVO;
import cn.tedu.hm.service.IOrderService;
import cn.tedu.hm.wep.JsonPage;
import cn.tedu.hm.wep.JsonResult;
import cn.tedu.hm.wep.WebConsts;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 说明:
 *
 * @ author: WeiZhen
 * @date: 2023/2/17
 **/
@Slf4j
@RestController
@RequestMapping("/orders")
@Api(tags = "02. 订单管理")
@Validated
@Transactional
public class OrderController {
    @Autowired
    private IOrderService orderService;

    //http://localhost:9085//orders/make-New
    @PreAuthorize("hasAuthority('/hm/order/add-new')")
    @PostMapping("/make-New")
    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
   public JsonResult<Void> makeNew(OrderMakeNewDTO orderMakeNewDTO){
        log.debug("开始执行生成订单的请求，参数为{}",orderMakeNewDTO);
        orderService.makeNew(orderMakeNewDTO);
        return JsonResult.ok();
    }
    //http://localhost:9085//orders/9527/delete
    @PreAuthorize("hasAuthority('/hm/order/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long", example = "4")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1,message = "请提供有效Id") Long id) {
        log.debug("开始处理【删除订单】的请求，ID：{}", id);
        orderService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9085/orders/9527/updateInfoById
    @PreAuthorize("hasAuthority('/hm/order/update')")
    @PostMapping("/{id:[0-9]+}/updateInfoById")
    @ApiOperation("根据id修改订单")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1,message = "请提供有效Id") Long id,
                                           @Valid OrderUpdateDTO orderUpdateDTO) {
        log.debug("开始处理【修改订单】的请求，ID：{}，新数据：{}", id, orderUpdateDTO);
        orderService.updateInfoById(id,orderUpdateDTO);
        return JsonResult.ok();
    }
    // http://localhost:9085/orders/9527
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询订单详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<OrderStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询订单详情】的请求，参数：{}", id);
        OrderStandardVO queryResult = orderService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
    // http://localhost:9085/orders
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    public JsonResult<JsonPage<OrderListItemVO>> list(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize){
        JsonPage<OrderListItemVO> list = orderService.list(page, pageSize);
        return JsonResult.ok(list);
    }
    // http://localhost:9085/orders/History
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("History")
    @ApiOperation("查询历史订单列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    public JsonResult<JsonPage<OrderListItemVO>> listHistory(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize){
        JsonPage<OrderListItemVO> list = orderService.listHistory(page, pageSize);
        return JsonResult.ok(list);
    }
    // http://localhost:9085/orders/selectByName
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("/selectByName")
    @ApiOperation("根据名字模糊查询订单列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "姓名",name = "name"),
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    public JsonResult<JsonPage<OrderListItemVO>> selectByName(
            @RequestParam()
                    String name,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize){
        JsonPage<OrderListItemVO> list = orderService.getListByName(name, page, pageSize);
        return JsonResult.ok(list);
    }
    // http://localhost:9085/orders/selectByPhone
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("/selectByPhone")
    @ApiOperation("根据电话模糊查询订单列表")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话",name = "phone"),
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    public JsonResult<JsonPage<OrderListItemVO>> selectByPhone(
            @RequestParam()
                    String phone,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize){
        JsonPage<OrderListItemVO> list = orderService.getListByPhone(phone, page, pageSize);
        return JsonResult.ok(list);
    }
    // http://localhost:9085/orders/selectByName
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("/selectHistoryByName")
    @ApiOperation("根据名字模糊查询历史订单列表")
    @ApiOperationSupport(order = 431)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "姓名",name = "name"),
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    public JsonResult<JsonPage<OrderListItemVO>> selectHistoryByName(
            @RequestParam()
                    String name,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize){
        JsonPage<OrderListItemVO> list = orderService.getHistoryListByName(name, page, pageSize);
        return JsonResult.ok(list);
    }

    // http://localhost:9085/orders/selectByPhone
    @PreAuthorize("hasAuthority('/hm/order/read')")
    @GetMapping("/selectHistoryByPhone")
    @ApiOperation("根据电话模糊查询订单列表")
    @ApiOperationSupport(order = 441)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话",name = "phone"),
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    public JsonResult<JsonPage<OrderListItemVO>> selectHistoryByPhone(
            @RequestParam()
                    String phone,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize){
        JsonPage<OrderListItemVO> list = orderService.getHistoryListByPhone(phone, page, pageSize);
        return JsonResult.ok(list);
    }
    // http://localhost:9085/orders/getOrderByCardNumber

    @GetMapping("/getOrderByCardNumber")
    @ApiOperation("根据身份证查询订单")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单人身份证",name = "cardNumber"),
            @ApiImplicitParam(value = "随从人身份证",name="otherCardNumber"),
    })
    public JsonResult<Void> getOrderByCardNumber(
            @RequestParam()
                    String cardNumber,
            @RequestParam()
                    String otherCardNumber) {
        log.debug("开始处理根据身份证查询订单的请求，参数：{}，{}", cardNumber, otherCardNumber);
        orderService.getOrderByCardNumber(cardNumber, otherCardNumber);
        return JsonResult.ok();
    }
}
